package com.lohika.util
{
	import com.lohika.Constant;
	
	import flash.media.Sound;
	import flash.text.Font;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;

	public class GameUtil
	{
		//http://www.spikything.com/blog/index.php/2009/09/06/how-to-embed-fonts-in-pure-as3/
		private static var font:flash.text.Font = new FontLucida();
		
		public static function getDefaultFontFormat(size:uint = 12):TextFormat
		{
			var bold:Boolean = font.fontStyle == 'bold'
			return new TextFormat(font.fontName, size, 0x0080C0, bold); //, , null, null, null, TextFormatAlign.CENTER);
		}
		
		
		public static function playSound(sound:Sound):void
		{
			sound.play();
		}
		
		public static function isPosibleMove(source:Array):Boolean
		{
			for(var col:int=0;col<Constant.COLUMNS;col++) 
			{     
				for(var  row:int=0;row<Constant.ROWS;row++) 
				{ 
					if (matchPattern(col, row, source, [[1,0]], [[-2,0],[-1,-1],[-1,1],[2,-1],[2,1],[3,0]])) {       
						return true;      
					}            
					
					if ( matchPattern(col, row, source,[[2,0]], [[1,-1],[1,1]] ) ) 
					{       
						return true;      
					}            
					
					if (matchPattern(col, row, source, [[0,1]], [[0,-2],[-1,-1],[1,-1],[-1,2],[1,2],[0,3]])) {       
						return true;      
					}            
					
					if (matchPattern(col, row, source,[[0,2]], [[-1,1],[1,1]])) {       
						return true;      
					}     
				}    
			}        
			
			return false;
		}
		
		
		/*inner*/
		private static function matchPattern(col:uint,row:uint, source:Array, mustHave:Array, needOne:Array):Boolean 
		{    
			var thisType:int = source[col][row].type;        
			
			for(var i:int=0;i<mustHave.length;i++) 
			{     
				if ( !matchType(col+mustHave[i][0], row+mustHave[i][1], thisType, source) ) 
				{      
					return false;     
				}    
			}        
			
			for(i=0;i<needOne.length;i++) 
			{     
				if (matchType(col+needOne[i][0], row+needOne[i][1], thisType, source)) 
				{      
					return true;     
				}    
			}    
			return false;   
		}
		
		private static function matchType(col:uint,row:uint,type:int, source:Array):Boolean 
		{    
			if ((col < 0) || (col > Constant.COLUMNS - 1) || (row < 0) || (row > Constant.ROWS - 1))
			{
				return false; //ignore out of borders items
			}
			return ( source[col][row].type == type );   
		} 
	}
}